#include "meter_dragonball.h"

namespace igslib
{
    int meter_Grgn::ID = -1;
    meter_Grgn::meter_Grgn()
    {
        ID++;
		LocID=ID;
        spi = nullptr;
        setup_ctrl[0]=1; 
    }
    meter_Grgn::~meter_Grgn()
    {
		if(ID>-1)
		ID--;
    }
    void meter_Grgn::SetSPI(ISpi* value)
    {
        spi = value;
    }		 
    int meter_Grgn::AddValue(const float& value)
    {
        int rtn = 0;
        if(value>65536)
        {
            util::Log("mater add  value which  is over 65536 per times"); 
            rtn = -1;
            return rtn;
        }
        Protocol_DragonBall* tmp = (Protocol_DragonBall*)(spi);
        tmp->SetMeter(LocID,value);
        this->value += value;
        return rtn;
    }

    int meter_Grgn::SetDutyRange(const float& act,const float& nact) 
    { 
        int rtn = 0;
        if(act<0 || act>65536)
        {
            rtn = -1;
            util::Log("meter SetDutyRange Setting over rage");
            return rtn;
        }
        if(nact<0 || nact>65536)
        {
            rtn = -1;
            util::Log("meter SetDutyRange Setting over rage");
            return rtn;
        }
        setup_ctrl[3]=act;
        setup_ctrl[4]=act/256;
        setup_ctrl[5]=nact;
        setup_ctrl[6]=nact/256;
        Protocol_DragonBall* tmp = (Protocol_DragonBall*)(spi);
        tmp->SetupMeter(LocID,setup_ctrl);
        return rtn;
    }
    void meter_Grgn::SetHighActive(const bool& value) 
    { 
        ha = value; 
        if( value==true )
            setup_ctrl[1]=1;
        else
            setup_ctrl[1]=0;
        Protocol_DragonBall* tmp = (Protocol_DragonBall*)(spi);
        tmp->SetupMeter(LocID,setup_ctrl);

    }		
    int meter_Grgn::SetGPIO(IGPIO* value) 
    { 
        int rtn = 0;
        gpio = value; 

        if(!gpio )
        {
            rtn = -1;
            util::Log("meter GPIO is NULL");			
            return rtn;
        }
        setup_ctrl[2]=(char)gpio->GetNumber();
        Protocol_DragonBall* tmp = (Protocol_DragonBall*)(spi);
        tmp->SetupMeter(LocID,setup_ctrl);
        return rtn;
    }	 
};
